import { createLogger } from 'redux-logger';

const actionTransformer = (action: any) => {
	if (action.type === 'BATCHING_REDUCER.BATCH') {
		action.payload.type = action.payload.map((next: any) => next.type).join(' => ');
		return action.payload;
	}

	return action;
};

const level = 'info';

const logger = {};

for (const method in console) {
	if (typeof console[method] === 'function') {
		logger[method] = console[method].bind(console);
	}
}

logger[level] = function levelFn(...args: any[]) {
	const lastArg = args.pop();

	if (Array.isArray(lastArg)) {
		return lastArg.forEach(item => {
			console[level].apply(console, [...args, item]);
		});
	}

	console[level].apply(console, arguments as any);
};

export default createLogger({
	level,
	actionTransformer,
	logger
});
